// 格式化日对象
import {
  analysis,
  calsulationSheBeiZhi,
  saveLdmzjz,
  getLdmzjz,
  calculation
} from '@/api/login'
import bus from '@/utils/eventBus'
export default {
  data() {
    return {
      emptyData: {
        xjr1: '',
        gdj1: '', // 真值设备A高低角
        du: '',
        fen: '',
        miao: '',
        duaxj: '', // 真值设备A舷角 - 度
        fenaxj: '',
        miaoaxj: '',
        du1: '',
        fen1: '',
        miao1: '',
        du2: '',
        fen2: '',
        miao2: '',
        xjq1: '',
        xb: '',
        yb: '',
        zb: ''
      },
      // 获取斜距参数
      xjParams: {
        // lon1: null,
        // lat1: null,
        // hgt1: null,
        // lon2: null,
        // lat2: null,
        // hgt2: null
      },
      tableData: [],
      json_fields: {
        '时间': 'czTime',
        '斜距R1(m)': 'xjr1',
        '高低角β1(°)': 'gdj1',
        '舷角q1(°)': 'xjq1',
        'Xb(m)': 'xb',
        'Yb(m)': 'yb',
        'Zb(m)': 'zb',
        '斜距R2(m)': 'xjr2',
        '高低角β2(°)': 'gdj2',
        '舷角q2(°)': 'xjq2',
        '被测设备测量值-斜距(m)': 'bcsbXj',
        '被测设备测量值-高低角(°)': 'bcsbGdj',
        '被测设备测量值-舷角(°)': 'bcsbXianJiao',
        '校准值-斜距(m)': 'jzzXj',
        '校准值-高低角(°)': 'jzzGdj',
        '校准值-舷角(°)': 'jzzXianJiao'
      },
      title: '瞄标定物数据信息表',
      // 无人机数据
      wrjData: {},
      spyData: {},
      isFlag: false
    }
  },
  created() {
    bus.$off('tabWrjXjToAddTabTwo')
    bus.$on('tabWrjXjToAddTabTwo', (val) => {
      // 接收从无人机页面添加的斜距信息到本页table中
      this.addXjToObjTable(val)
    })
  },
  mounted() {
  },
  methods: {
    getNowDate() {
      var date = new Date()
      var sign2 = ':'
      var year = date.getFullYear() // 年
      var month = date.getMonth() + 1 // 月
      var day = date.getDate() // 日
      var hour = date.getHours() // 时
      var minutes = date.getMinutes() // 分
      var seconds = date.getSeconds() // 秒
      var weekArr = ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期天']
      var week = weekArr[date.getDay()]
      // 给一位数的数据前面加 “0”
      if (month >= 1 && month <= 9) {
        month = '0' + month
      }
      if (day >= 0 && day <= 9) {
        day = '0' + day
      }
      if (hour >= 0 && hour <= 9) {
        hour = '0' + hour
      }
      if (minutes >= 0 && minutes <= 9) {
        minutes = '0' + minutes
      }
      if (seconds >= 0 && seconds <= 9) {
        seconds = '0' + seconds
      }
      return hour + sign2 + minutes + sign2 + seconds
    },
    /**
     * 往表格中添加一条斜距信息
     */
    addXjToObjTable(val) {
      this.emptyData = {
        czTime: this.getNowDate(),
        // 斜距
        xjr1: val,
        gdj1: '', // 真值设备A高低角
        du: '',
        fen: '',
        miao: '',
        duaxj: '', // 真值设备A舷角 - 度
        fenaxj: '',
        miaoaxj: '',
        xjq1: '',
        xb: '',
        yb: '',
        zb: '',
        bcsbXj: '',
        bcsbGdj: '',
        bcsbXianJiao: '',
        xjr2: '',
        gdj2: '',
        xjq2: '',
        jzzXj: '',
        jzzGdj: '',
        jzzXianJiao: ''
      }
      this.tableData.push(this.emptyData)
      this.tableData = JSON.parse(JSON.stringify(this.tableData))
    },
    // 添加表单项
    addForm() {
      if (this.isFlag) {
        this.tableData = []
        this.isFlag = false
      }
      this.xjParams = {
        lon1: this.$store.state.wrjLon || 0,
        lat1: this.$store.state.wrjLat || 0,
        hgt1: this.$store.state.wrjHgt || 0,
        lon2: this.$store.state.spyLon || 0,
        lat2: this.$store.state.spyLat || 0,
        hgt2: this.$store.state.spyHgt || 0
      }
      // 获取斜距并添加一条数据至表格
      calculation(this.xjParams, true).then(res => {
        if (res.code === 0) {
          console.log(res, '斜距res')
          if (this.tableData.length < 1) {
            this.addXjToObjTable(res.data)
          } else {
            // console.log(this.tableData[this.tableData.length - 1])
            this.emptyData = {
              czTime: this.getNowDate(),
              // 斜距
              xjr1: res.data,
              gdj1: '', // 真值设备A高低角
              du: '',
              fen: '',
              miao: '',
              duaxj: '', // 真值设备A舷角 - 度
              fenaxj: '',
              miaoaxj: '',
              du1: '',
              fen1: '',
              miao1: '',
              du2: '',
              fen2: '',
              miao2: '',
              xjq1: '',
              xb: this.tableData[this.tableData.length - 1].xb,
              yb: this.tableData[this.tableData.length - 1].yb,
              zb: this.tableData[this.tableData.length - 1].zb,
              bcsbXj: '',
              bcsbGdj: '',
              bcsbXianJiao: '',
              xjr2: '',
              gdj2: '',
              xjq2: '',
              jzzXj: '',
              jzzGdj: '',
              jzzXianJiao: ''
            }
            this.tableData.push(this.emptyData)
            this.tableData = JSON.parse(JSON.stringify(this.tableData))
          }
        }
      })
      // console.log(this.wrjData.height, 'this.wrjdata.height')
    },
    convertDMSToDecimal(degree, minute, second) {
      // 确保输入值是在有效的范围内
      if (degree < -360 || degree > 360 || minute < -60 || minute >= 60 || second < -60 || second >= 60) {
        return 0
      }
      // 计算十进制度
      let decimalDegree = (+degree) + (+minute) / 60 + (+second) / 3600
      // 如果原始度数是负数，则十进制度也应该是负数
      if (degree < 0) {
        decimalDegree = -decimalDegree
      }
      return parseFloat(decimalDegree.toFixed(4))
    },
    convertDecimalToDMS(decimalDegree) {
      // 确保输入值是在有效的经纬度范围内
      if (decimalDegree < -360 || decimalDegree > 360) {
        return {
          degree: 0,
          minute: 0,
          second: 0
        }
      }
      // 计算度
      let degree = Math.floor(decimalDegree)
      // 计算分
      let minuteNotTruncated = Math.abs(decimalDegree - degree) * 60
      let minute = Math.floor(minuteNotTruncated)
      // 计算秒
      let second = Math.round((minuteNotTruncated - minute) * 60)
      // 返回度分秒对象
      return {
        degree: degree,
        minute: minute,
        second: second
      }
    },
    // 计算分析
    clickAnalysis() {
      analysis(this.tableData).then(res => {
        if (res.code === 0) {
          this.tableData = res.data
        }
      })
      const trueOrFalse = this.tableData.every((item) => {
        return item.id
      })
      console.log(trueOrFalse)
      if (!trueOrFalse) {
        calsulationSheBeiZhi(this.tableData).then(res => {
          if (res.code === 0) {
            this.tableData = res.data.map(item => {
              console.log('--- item ---', item)
              item.gdj1 = this.convertDMSToDecimal(+item.du, +item.fen, +item.miao)
              item.duaxj = this.convertDecimalToDMS(+item.xjq1).degree
              item.fenaxj = this.convertDecimalToDMS(+item.xjq1).minute
              item.miaoaxj = this.convertDecimalToDMS(+item.xjq1).second
              item.du1 = this.convertDecimalToDMS(+item.bcsbGdj).degree
              item.fen1 = this.convertDecimalToDMS(+item.bcsbGdj).minute
              item.miao1 = this.convertDecimalToDMS(+item.bcsbGdj).second
              item.du2 = this.convertDecimalToDMS(+item.bcsbXianJiao).degree
              item.fen2 = this.convertDecimalToDMS(+item.bcsbXianJiao).minute
              item.miao2 = this.convertDecimalToDMS(+item.bcsbXianJiao).second
              return item
            })
            console.log('this.tableData ------', this.tableData)
          }
        })
      } else {
        this.$message({
          message: '查看历史时无需计算,请正确添加！',
          type: 'warning',
          center: true
        })
      }
    },
    // 保存分析结果
    save() {
      saveLdmzjz(this.tableData).then(res => {
        if (res.code === 0) {
          this.$message.success('保存成功')
        }
      })
    },
    // 获取历史分析结果
    getLdmzjz() {
      this.isFlag = true
      getLdmzjz().then(res => {
        if (res.code === 0) {
          this.tableData = res.data
        }
      })
    },
    // 删除单行
    del(index) {
      this.tableData.splice(index, 1)
    }
  }
}
